import { defineConfig } from 'vite'
// import { viteObfuscateFile } from 'vite-plugin-obfuscator'
import obfuscatorPlugin from "vite-plugin-javascript-obfuscator";
// import mkcert from "vite-plugin-mkcert";
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import viteCompression from 'vite-plugin-compression';
// import { visualizer } from 'rollup-plugin-visualizer'
import path from 'path'
export default defineConfig({
  plugins: [
    vue(),
    // mkcert(), 
    // visualizer({ open: true }),
    viteCompression({
      verbose: true, // 默认即可
      disable: false, //开启压缩(不禁用)，默认即可
      deleteOriginFile: false, //删除源文件
      threshold: 10240, //压缩前最小文件大小
      algorithm: 'gzip', //压缩算法
      ext: '.gz', //文件类型
    }),
    Components({
      resolvers: [VantResolver()],
    }),
    obfuscatorPlugin({
      include: ["src/utils/config.js"],
      exclude: [/node_modules/],
      apply: "build",
      options: {
        // your javascript-obfuscator options
        // debugProtection: true,
        // ...  [See more options](https://github.com/javascript-obfuscator/javascript-obfuscator)
      },
    }),
  ],
  define: {
    "process.env": {}
  },
  resolve: {
    alias: { '@': path.resolve(__dirname, 'src') }
  },

  css: {
    preprocessorOptions: {
      // 全局样式引入
      scss: {
        additionalData: '@import "/src/assets/theme.scss";',
        javascriptEnabled: true
      }
    }
  },
  server: {
    host: "0.0.0.0",
    // https: true,
  },
  build: {
    minify: 'terser',
    terserOptions: {
      // 清除console和debugger
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: {
        chunkFileNames: 'js/[name]-[hash].js',
        entryFileNames: 'js/[name]-[hash].js',
        assetFileNames: '[ext]/[name]-[hash].[ext]'
      },
      // manualChunks(id) {
      //   if (id.includes("node_modules")) {
      //     // 让每个插件都打包成独立的文件
      //     return id.toString().split("node_modules/")[1].split("/")[0].toString();
      //   }
      // }
    }
  },
})
